package com.sdkj.controller.law;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.sdkj.base.common.FileDownload;
import com.sdkj.base.common.FileUpload;
import com.sdkj.controller.base.BaseController;
import com.sdkj.entity.Page;
import com.sdkj.service.law.LawAskService;
import com.sdkj.util.Const;
import com.sdkj.util.DateUtil;
import com.sdkj.util.Jurisdiction;
import com.sdkj.util.PageData;
import com.sdkj.util.PathUtil;

@Controller
@RequestMapping(value="/lawask")
public class LawAskController extends BaseController{
	String menuUrl = "lawask/list.do"; //菜单地址(权限用)
	@Resource(name="lawAskService")
	private LawAskService lawAskService;
	
	/**
	 * 列表
	 */
	@RequestMapping(value="/list")
	public ModelAndView list(Page page){
		logBefore(logger, "列表");
		//if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			page.setPd(pd);
			List<PageData>	varList = lawAskService.list(page);	//列出LawAsk列表
			mv.setViewName("law/lawAsk/lawAsk_list");
			mv.addObject("varList", varList);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX,this.getHC());	//按钮权限
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/*
	 * 去往新增页面
	 */
	@RequestMapping(value="/goAdd")
	public ModelAndView goAdd(){
		logBefore(logger, "去新增");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			mv.setViewName("law/lawAsk/lawAsk_add");
			mv.addObject("msg", "save");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}
	
	/**
	 * 保存
	 */
	
	@RequestMapping(value="/save")
	public ModelAndView save(MultipartFile file,HttpServletRequest request) throws Exception{
		logBefore(logger, "新增Lawask");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData(request);
		String time = DateUtil.getDay();
		String filePath = null;
		String fileName = null;
		if (null != file && !file.isEmpty()) {
			filePath =PathUtil.getClasspath()+ Const.FILEPATHFILE+time;
			fileName = FileUpload.fileUpload(file, filePath, this.get32UUID());	
		}else {
			System.out.println("上传失败");
		}
		pd.put("ask_id", this.get32UUID());	//主键
		pd.put("user_id", getCurrentUser().getUSER_ID());
		pd.put("ask_person", getCurrentUser().getUSERNAME());
		pd.put("ask_status", 0 );
		pd.put("dept_id", getCurrentUser().getDepartmentId());
		pd.put("company_id", getCurrentUser().getCompanyId());
		pd.put("ask_attachment",filePath);
		pd.put("ask_name", fileName);
		lawAskService.save(pd);
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	/**
	 * 去修改页面
	 */
	@RequestMapping(value="/goEdit")
	public ModelAndView goEdit(){
		logBefore(logger, "去修改LawAsk页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			pd = lawAskService.findById(pd);	//根据ID读取
			mv.setViewName("law/lawAsk/lawAsk_edit");
			mv.addObject("msg", "edit");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}
	
	/**
	 * 修改
	 */
	@RequestMapping(value="/edit")
	public ModelAndView edit(MultipartFile file,HttpServletRequest request) throws Exception{
		logBefore(logger, "修改LawAsk");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData(request);
		PageData pd2 = new PageData();
		String time = DateUtil.getDay();
		String filePath = null;
		String fileName = null;
		if (null != file && !file.isEmpty()) {
			filePath =PathUtil.getClasspath()+ Const.FILEPATHFILE+time;
			fileName = FileUpload.fileUpload(file, filePath, this.get32UUID());	
		}else {
			System.out.println("上传失败");
		}
			if (pd.getString("ask_status").equals("0")) {
				pd2.put("answer_id", this.get32UUID());
				pd2.put("user_id", getCurrentUser().getUSER_ID());
				pd2.put("answer_person", getCurrentUser().getUSERNAME());
				pd2.put("ask_id", pd.get("ask_id"));
				pd2.put("answer_attachment", filePath);
				pd2.put("answer_describe", pd.get("answer_describe"));
				pd2.put("dept_id", getCurrentUser().getDepartmentId());
				pd2.put("company_id", getCurrentUser().getCompanyId());
				pd.put("ask_status", 1);
				pd2.put("answer_name", fileName);
				lawAskService.saveAs(pd2);
				lawAskService.updateAsk(pd);
			}
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	
	/*
	 * 下载功能
	 */
	@RequestMapping(value="download")
	public void download(HttpServletResponse response)throws Exception{
		logBefore(logger, "下载功能");
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {return;}
		PageData pd = new PageData();
		pd = this.getPageData();
		pd = lawAskService.findById(pd);
		String filePath = (String) pd.get("ask_attachment");
		String fileName = (String) pd.get("ask_name");
		FileDownload.fileDownload(response, filePath+"/"+fileName, fileName);
	}
	
	@RequestMapping(value="download1")
	public void download1(HttpServletResponse response)throws Exception{
		logBefore(logger, "下载功能");
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {return;}
		PageData pd = new PageData();
		pd = this.getPageData();
		pd = lawAskService.findById(pd);
		String filePath = (String) pd.get("answer_attachment");
		String fileName = (String) pd.get("answer_name");
		FileDownload.fileDownload(response, filePath+"/"+fileName, fileName);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/* ===============================权限================================== */
	public Map<String, String> getHC(){
		Subject currentUser = SecurityUtils.getSubject();  //shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>)session.getAttribute(Const.SESSION_QX);
	}
/* ===============================权限================================== */
	
	@InitBinder
	public void initBinder(WebDataBinder binder){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(format,true));
	}
	
	
}
